package service

import (
	"aigen-todo/internal/interfaces"
	"aigen-todo/internal/models"
	"time"
)

type TaskGroupService struct {
	taskGroupRepo interfaces.TaskGroupRepositoryInterface
	userRepo      interfaces.UserRepositoryInterface
}

func NewTaskGroupService(taskGroupRepo interfaces.TaskGroupRepositoryInterface, userRepo interfaces.UserRepositoryInterface) *TaskGroupService {
	return &TaskGroupService{
		taskGroupRepo: taskGroupRepo,
		userRepo:      userRepo,
	}
}

func (s *TaskGroupService) CreateTaskGroup(userID uint64, name string) (*models.TaskGroup, error) {
	// 检查用户是否存在
	_, err := s.userRepo.GetByID(userID)
	if err != nil {
		return nil, err
	}

	// 创建任务分组
	group := &models.TaskGroup{
		UserID: userID,
		Name:   name,
	}

	err = s.taskGroupRepo.Create(group)
	if err != nil {
		return nil, err
	}

	return group, nil
}

func (s *TaskGroupService) GetTaskGroupByID(id uint64) (*models.TaskGroup, error) {
	return s.taskGroupRepo.GetByID(id)
}

func (s *TaskGroupService) GetTaskGroupsByUserID(userID uint64) ([]models.TaskGroup, error) {
	return s.taskGroupRepo.GetByUserID(userID)
}

func (s *TaskGroupService) UpdateTaskGroup(group *models.TaskGroup) error {
	group.UpdatedAt = time.Now()
	return s.taskGroupRepo.Update(group)
}

func (s *TaskGroupService) DeleteTaskGroup(id uint64) error {
	return s.taskGroupRepo.Delete(id)
}
